const express = require("express");
const _ = require("lodash");
const areaData = require("../modules/areaData.js");
const router = express.Router();

//获取省
router.get("/getProvinces", (req,res,next)=>{
	let provinces = _.keys(areaData);
	res.json({code: 0, result: provinces});
});

//获取市
router.get("/getCities", (req,res,next)=>{
	let province = req.query.province;
	if(!province) return res.json({code: 1001, errorMsg: "缺少必要参数"});

	let provinces = _.keys(areaData);
	if(provinces.indexOf(province) < 0) return res.json({code: 1002, errorMsg: "省份不正确"});

	let cities = _.keys(areaData[province]);
	res.json({code: 0, result: cities});
});

//获取区
router.get("/getAreas", (req, res, next)=>{
	let province = req.query.province;
	let city = req.query.city;
	if(!province || !city) return res.json({code: 1001, errorMsg: "缺少必要参数"});
	
	let provinces = _.keys(areaData);
	if(provinces.indexOf(province) < 0) return res.json({code: 1002, errorMsg: "省份错误"});

	let cities = _.keys(areaData[province]);
	if(cities.indexOf(city) < 0) return res.json({code: 1003, errorMsg: "市不正确"});
	
	let areas = areaData[province][city];
	res.json({code: 0, result: areas});
});


module.exports = router;